package com.youlu.campus.common.base;

import com.youlu.campus.common.exception.ApiErrorCode;
import com.youlu.campus.common.model.GlobalErrorCode;

/**
 * RestResult Builder
 * <p>
 * Created by zhuhuaiqi
 */
@Deprecated
public class RestResultBuilder {

    protected int code;

    protected String message;

    protected String msg;

    protected Object data;

    public static RestResultBuilder builder() {
        RestResultBuilder restResultBuilder = new RestResultBuilder();
        return restResultBuilder;
    }

    public RestResultBuilder code(int code) {
        this.code = code;
        return this;
    }

    public RestResultBuilder message(String message) {
        this.message = message;
        this.msg = message;
        return this;
    }

    public RestResultBuilder data(Object data) {
        this.data = data;
        return this;
    }

    public RestResultBuilder errorCode(ApiErrorCode errorCode) {
        this.code = errorCode.getCode();
        this.message = errorCode.getMessage();
        this.msg = errorCode.getMessage();
        return this;
    }

    public RestResultBuilder success() {
        this.code = GlobalErrorCode.SUCCESS.getCode();
        this.message = GlobalErrorCode.SUCCESS.getMessage();
        this.msg = GlobalErrorCode.SUCCESS.getMessage();
        return this;
    }

    public RestResultBuilder success(Object data) {
        this.code = GlobalErrorCode.SUCCESS.getCode();
        this.message = GlobalErrorCode.SUCCESS.getMessage();
        this.msg = GlobalErrorCode.SUCCESS.getMessage();
        this.data = data;
        return this;
    }

    public RestResultBuilder failure() {
        this.code = GlobalErrorCode.FAIL.getCode();
        this.message = GlobalErrorCode.FAIL.getMessage();
        this.msg =  GlobalErrorCode.FAIL.getMessage();

        return this;
    }

    public RestResultBuilder failure(Object data) {
        this.code = GlobalErrorCode.FAIL.getCode();
        this.message = GlobalErrorCode.FAIL.getMessage();
        this.msg = GlobalErrorCode.FAIL.getMessage();
        this.data = data;
        return this;
    }

    public RestResultBuilder result(boolean successful) {
        if (successful) {
            return this.success();
        } else {
            return this.failure();
        }
    }

    public RestResultBuilder success(Boolean result) {
        if (result.equals(Boolean.TRUE)) {
            success();
        } else {
            failure();
        }
        return this;
    }

    public RestResult build() {
        return new RestResult(this.code, this.message, this.data);
    }

    public RestResult build(RestResult restResult) {
        return restResult;
    }
}
